﻿using System.Linq;
using System.Configuration;
using GlobalOfficeTools.MVC3.Models;

namespace GlobalOfficeTools.MVC3.DomainServices.Clients
{
    public partial class ClientsDomainService
    {
        #region Override Methods 
        protected override ClientsEntities CreateObjectContext()
        {
            string strConnectionString = string.Format("metadata=res://*/Models.ClientsModel.csdl|res://*/Models.ClientsModel.ssdl|res://*/Models.ClientsModel.msl;provider=System.Data.SqlClient;provider connection string=\"{0}\"", ConfigurationManager.ConnectionStrings["GlobalOfficeToolsEntities"]);
            return new ClientsEntities(strConnectionString);
        }
        #endregion

        #region Public Methods 
        public IQueryable<CLItblUser> GetUser(string userName, string password, string federationType)
        {
            // TODO: the Password management it's wrong. Need to fix it later 2011/10/02
            var federationDetail = (from x in this.ObjectContext.CLItblFederationDetails
                                   where 
                                    x.UserName == userName &&
                                    x.Password == password &&
                                    x.FederationType == federationType
                                   select x).FirstOrDefault();

            return from x in this.ObjectContext.CLItblUsers.Include("CLItblUserOption")
                   where x.UserId == federationDetail.UserId
                   select x;
        }
        public IQueryable<CLIvwEmployeeType> GetEmployeeType(int userId, int companyId)
        {
            var a = from employeeType in this.ObjectContext.CLIvwEmployeeTypes
                   join companyUser in this.ObjectContext.CLItblCompanyUsers on employeeType.EmployeeTypeId equals companyUser.EmployeeTypeId
                   where
                    companyUser.CompanyId == companyId &&
                    companyUser.UserId == userId
                   select employeeType;
            return a;
        }
        public IQueryable<CLItblCompany> GetCompany(int companyId) 
        {
            return from x in this.ObjectContext.CLItblCompanies.Include("CLItblCompanyOption")
                   where x.CompanyId == companyId
                   select x;
        }

        public IQueryable<CLItblAcl> GetAccessListTable(int userId, int companyId)
        {
            return from x in this.ObjectContext.CLItblAcls 
                   where
                    x.CompanyId == companyId &&
                    x.Entity == "USER" && 
                    x.EntityId == userId
                   select x;

            //return companyAcl.Union(userAcl);
        }
        #endregion
    }
}